package cn.lastcrdg.testslide;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.List;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/10/17 17:14
 * description：
 */

public class RefreshAdapter extends RecyclerView.Adapter<RefreshAdapter.MyHolder> {
    private static final String TAG = RefreshAdapter.class.getSimpleName();
    private Context context;
    private List<String[]> list;//数据
    private int max_count = 1; //最大的显示数
    private Boolean isFootView = false;//是否添加FootView
    private String footViewText = ""; //Footview的内容

    //两个final int类型表示ViewType的两种类型
    private final int NORMAL_TYPE = 0;
    private final int FOOT_TYPE = 1111;
    private boolean isTwoData;

    public RefreshAdapter(Context context, List<String[]> data, boolean isTwoData) {
        this.list = data;
        this.context = context;
        this.isTwoData = isTwoData;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == NORMAL_TYPE) {
            View normal_views = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_adapter, parent, false);
            return new MyHolder(normal_views, NORMAL_TYPE);
        } else {
            View foot_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bottom, parent, false);
            return new MyHolder(foot_view, FOOT_TYPE);
        }
    }

    @Override
    public void onBindViewHolder(MyHolder mHolder, final int position) {
        //建立起ViewHolder中试图与数据的关联
        //如果footview存在，并且当前位置ViewType是FOOT_TYPE
        if (isFootView && (getItemViewType(position) == FOOT_TYPE)) {
            if (position == list.size()){
                mHolder.tvFootView.setText("没有更多了");
            }else {
                mHolder.tvFootView.setText(footViewText);
                // 刷新太快 所以使用Hanlder延迟两秒
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        max_count += 1;
//                        notifyDataSehanged();
                        notifyItemInserted(list.size());
                    }
                }, 2000);
            }
        } else {
            Log.d(TAG, "onBindViewHolder: " + position);
            mHolder.tv1.setText(list.get(position)[0]);
            mHolder.tv2.setText(list.get(position)[1]);
            mHolder.tv3.setText(list.get(position)[2]);
            mHolder.tv4.setText(list.get(position)[3]);
            mHolder.tv5.setText(list.get(position)[4]);
            mHolder.tv6.setText(list.get(position)[5]);
            if (position + 1 == list.size()) {
                if (isTwoData) {
                    mHolder.mLinearLayout.setVisibility(View.VISIBLE);
                    mHolder.tv12.setText(list.get(position)[6]);
                    mHolder.tv22.setText(list.get(position)[7]);
                    mHolder.tv32.setText(list.get(position)[8]);
                    mHolder.tv42.setText(list.get(position)[8]);
                    mHolder.tv52.setText(list.get(position)[10]);
                    mHolder.tv62.setText(list.get(position)[11]);
                } else {
                    mHolder.mLinearLayout.setVisibility(View.GONE);
                }
            }else {
                mHolder.mLinearLayout.setVisibility(View.VISIBLE);
                mHolder.tv12.setText(list.get(position)[6]);
                mHolder.tv22.setText(list.get(position)[7]);
                mHolder.tv32.setText(list.get(position)[8]);
                mHolder.tv42.setText(list.get(position)[8]);
                mHolder.tv52.setText(list.get(position)[10]);
                mHolder.tv62.setText(list.get(position)[11]);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == max_count) {
            return FOOT_TYPE;
        }
        return NORMAL_TYPE;
    }

    @Override
    public int getItemCount() {
        if (list.size() < max_count) {
            return list.size() + 1;
        }
        return max_count + 1;  // 这个地方必须要+1  不然加载的条目只有max_cout个，不能加载下拉刷新那一条
    }

    //创建一个方法来设置footView中的文字
    public void setFootViewText(String footViewText) {
        isFootView = true;
        this.footViewText = footViewText;
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private TextView tv1;
        private TextView tv2;
        private TextView tv3;
        private TextView tv4;
        private TextView tv5;
        private TextView tv6;
        private TextView tv12;
        private TextView tv22;
        private TextView tv32;
        private TextView tv42;
        private TextView tv52;
        private TextView tv62;
        private LinearLayout mLinearLayout;
        private ScrollView mScroll;
        public TextView tvFootView;//footView的TextView属于独自的一个layout

        public MyHolder(View view, int viewType) {
            super(view);
            if (viewType == NORMAL_TYPE) {
                tv1 = view.findViewById(R.id.tv1);
                tv2 = view.findViewById(R.id.tv2);
                tv3 = view.findViewById(R.id.tv3);
                tv4 = view.findViewById(R.id.tv4);
                tv5 = view.findViewById(R.id.tv5);
                tv6 = view.findViewById(R.id.tv6);
                tv12 = view.findViewById(R.id.tv12);
                tv22 = view.findViewById(R.id.tv22);
                tv32 = view.findViewById(R.id.tv32);
                tv42 = view.findViewById(R.id.tv42);
                tv52 = view.findViewById(R.id.tv52);
                tv62 = view.findViewById(R.id.tv62);
                mLinearLayout = view.findViewById(R.id.ll_second);
                mScroll = view.findViewById(R.id.sv);
            } else {
                tvFootView = (TextView) view.findViewById(R.id.tv_bottom);
            }
        }
    }
}